﻿using lwzlz.Application.System;
using lwzlz.Common;
using lwzlz.Web.Areas.SysAdmin.Models;
using lzwlz.Web;
using System;
using System.Web.Mvc;

namespace lwzlz.Web.Areas.Mall.Controllers
{
    /// <summary>
    /// 企业首页
    /// </summary>
    public class HomeController : MallController
    {
        #region 界面

        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ResetPW()
        {
            return View();
        }
        #endregion

        #region 接口

        /// <summary>
        /// 登出
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SignOut()
        {
            ResponseModel response = new ResponseModel();
            try
            {
                MallMemberLogin.SingOut();

                response.Data = Url.RouteUrl(new { area = "Mall", controller = "Login", action = "Index" });
                response.Success = true;
                response.Message = "登出成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult RestPassWord(int id, string oldPassWord, string firstPassWord, string secondPassWord)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                var memberService = new MemberService();
                var entity = memberService.Get(id);
                if (entity != null)
                {
                    if (entity.PassWord == new Symmetric().Encrypto(oldPassWord))
                    {
                        if (firstPassWord == secondPassWord)
                        {
                            var result = memberService.UpdatePassWord(id, new Symmetric().Encrypto(firstPassWord));

                            if (result.Success)
                            {
                                MallMemberLogin.SingOut();

                                response.Data = Url.RouteUrl(new { area = "Mall", controller = "Login", action = "Index" });
                                response.Success = true;
                                response.Message = "重置成功，请重新登录";
                            }
                            else
                            {
                                response.Success = false;
                                response.Message = result.Message;
                            }
                        }
                        else
                        {
                            response.Success = false;
                            response.Message = "两次密码不一致";
                        }
                    }
                    else
                    {
                        response.Success = false;
                        response.Message = "旧登录密码错误";
                    }
                }
                else
                {
                    response.Success = false;
                    response.Message = "账号错误";
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }

        #endregion
    }
}